package com.simple.portal.bo.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.simple.portal.bo.IGroupRightRelBO;
import com.simple.portal.dao.IGroupRightRelDAO;
import com.simple.portal.dao.IInitRightDAO;
import com.simple.portal.vo.GroupRightRelVO;
import com.simple.portal.vo.InitRightVO;
import com.simple.util.common.Constants;
import com.simple.util.object.portal.SessionUserBean;

@Service("groupRightRelBO")
@Transactional(propagation=Propagation.REQUIRED,readOnly=true)
public class GroupRightRelBOImpl implements IGroupRightRelBO{
	
	@Autowired
	private IGroupRightRelDAO groupRightRelDAO;
	
	@Autowired
	private IInitRightDAO initRightDAO;
	
	@Override
	public GroupRightRelVO findById(String id) {
		
		return groupRightRelDAO.findById(id);
	}

	@Override
	public List<GroupRightRelVO> findAll() {
		
		return groupRightRelDAO.findAll();
	}
	
	@Transactional(propagation=Propagation.REQUIRED,readOnly=false,rollbackFor={Exception.class})
	@Override
	public void saveAuthorization(final String groupId, final String organizationType, final String adminFlag, final String[] operationCheckbox, final String[] authorizationCheckbox, final SessionUserBean currentSessionUserBean){
		final List<GroupRightRelVO> groupRightRelVOListO = groupRightRelDAO.findAllByGroupIdAndRightFlag(groupId, Constants.GROUP_RIGHT_FLAG_OPERATION);
		final List<GroupRightRelVO> groupRightRelVOListA = groupRightRelDAO.findAllByGroupIdAndRightFlag(groupId, Constants.GROUP_RIGHT_FLAG_AUTHORIZATION);
		
		List<String> operationList;
		if(operationCheckbox == null){
			operationList = new ArrayList<String>();
		}else{
			operationList = new ArrayList<String>(Arrays.asList(operationCheckbox));
		}
		List<String> authorizationList;
		if(authorizationCheckbox == null){
			authorizationList = new ArrayList<String>();
		}else{
			authorizationList = new ArrayList<String>(Arrays.asList(authorizationCheckbox));
		}
		
		final List<InitRightVO> initRightVOList = initRightDAO.findInitRightVOList(organizationType, adminFlag);
		for(InitRightVO initRightVO : initRightVOList){
			if(Constants.GROUP_RIGHT_FLAG_OPERATION.equals(initRightVO.getId().getRightFlag())){
				operationList.add(initRightVO.getId().getRightId());
			}
			
			if(Constants.GROUP_RIGHT_FLAG_AUTHORIZATION.equals(initRightVO.getId().getRightFlag())){
				authorizationList.add(initRightVO.getId().getRightId());
			}
		}
		
		Boolean temp;
		GroupRightRelVO groupRightRelVO;
		
		for(String operationStr : operationList){
			temp = true;
			final Iterator<GroupRightRelVO> rightInfoVOListOIterator = groupRightRelVOListO.iterator();
			
			while(rightInfoVOListOIterator.hasNext()){
				groupRightRelVO = rightInfoVOListOIterator.next();
				if(operationStr.equals(groupRightRelVO.getRightId())){
					temp = false;
					rightInfoVOListOIterator.remove();
					break;
				}
			}
			if(temp){
				groupRightRelVO = new GroupRightRelVO();
				groupRightRelVO.setGroupId(groupId);
				groupRightRelVO.setRightFlag(Constants.GROUP_RIGHT_FLAG_OPERATION);
				groupRightRelVO.setRightId(operationStr);
				groupRightRelDAO.save(groupRightRelVO, currentSessionUserBean.getUserInfoVO().getUserId());
			}
		}
		
		for(GroupRightRelVO grrv : groupRightRelVOListO){
			groupRightRelDAO.delete(grrv, currentSessionUserBean.getUserInfoVO().getUserId());
		}
		
		
		for(String authorizationStr : authorizationList){
			temp = true;
			final Iterator<GroupRightRelVO> rightInfoVOListAIterator = groupRightRelVOListA.iterator();
			
			while(rightInfoVOListAIterator.hasNext()){
				groupRightRelVO = rightInfoVOListAIterator.next();
				if(authorizationStr.equals(groupRightRelVO.getRightId())){
					temp = false;
					rightInfoVOListAIterator.remove();
					break;
				}
			}
			if(temp){
				groupRightRelVO = new GroupRightRelVO();
				groupRightRelVO.setGroupId(groupId);
				groupRightRelVO.setRightFlag(Constants.GROUP_RIGHT_FLAG_AUTHORIZATION);
				groupRightRelVO.setRightId(authorizationStr);
				groupRightRelDAO.save(groupRightRelVO, currentSessionUserBean.getUserInfoVO().getUserId());
			}
		}
		
		for(GroupRightRelVO grrv : groupRightRelVOListA){
			groupRightRelDAO.delete(grrv, currentSessionUserBean.getUserInfoVO().getUserId());
		}
	}
	
	

}
